package info.ericbutler.microcosm.game;

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.util.Log;

public class MainMenuState extends BasicGameState {
	private int id;
	private Image background;
	private int currentState;
	private static final int PRE_CLICK = 0;
	private static final int POST_CLICK = 1;
	private static final int MULTIPLAYER_CLICK = 2;
	private static final int CONNECT_CLICK = 3;
	private static final int OPTIONS_CLICK = 9;
	private Rectangle singlePlayerClick, multiPlayerClick, optionsClick;
	
	public MainMenuState(int MAINMENU_STATE) {
		this.id = MAINMENU_STATE;
	}

	@Override
	public int getID() {
		return id;
	}

	@Override
	public void init(GameContainer container, StateBasedGame game) throws SlickException {
		Log.info("Loading Main Menu background");
		currentState = PRE_CLICK;
		background = new Image( "rsc/images/mainmenu_1024x768.jpg" );
		singlePlayerClick = new Rectangle(325, 590, 409, 40);
		multiPlayerClick = new Rectangle(325, 635, 383, 40);
		optionsClick = new Rectangle(325, 680, 213, 40);
	}

	@Override
	public void update(GameContainer container, StateBasedGame game, int delta) throws SlickException {
		Input inputs = container.getInput();
		boolean isLeftClick = inputs.isMousePressed(0);
		float mX = 0, mY = 0;
		if( isLeftClick ) {
			mX = inputs.getAbsoluteMouseX();
			mY = inputs.getAbsoluteMouseY();
		}
		switch( currentState ) {
			case PRE_CLICK:
				if( isLeftClick ) {
					currentState = POST_CLICK;
				}
				break;
			case POST_CLICK:
				if( singlePlayerClick.contains(mX, mY) ) {
					game.enterState(MicrocosmMain.SINGLE_PLAYER_STATE);
				} else if( multiPlayerClick.contains(mX, mY) ) {
					currentState = MULTIPLAYER_CLICK;
				} else if( optionsClick.contains(mX, mY) ) {
					currentState = OPTIONS_CLICK;
				}
				break;
			case MULTIPLAYER_CLICK:
				break;
			case CONNECT_CLICK:
				break;
			case OPTIONS_CLICK:
				break;
		}
		
	}
	
	@Override
	public void render(GameContainer container, StateBasedGame game, Graphics g) throws SlickException {
		g.drawImage(background, 0, 0, MicrocosmMain.SCREEN_WIDTH, MicrocosmMain.SCREEN_HEIGHT, 0, 0, background.getWidth(), background.getHeight());
		switch( currentState ) {
			case PRE_CLICK:
				MicrocosmMain.GAME_FONT.drawString(325, 600, "Click to start", Color.white);
				break;
			case POST_CLICK:
				MicrocosmMain.GAME_FONT.drawString(325, 590, "Single Player", Color.white);
				MicrocosmMain.GAME_FONT.drawString(325, 635, "Multi-Player", Color.white);
				MicrocosmMain.GAME_FONT.drawString(325, 680, "Options", Color.white);
				break;
			case MULTIPLAYER_CLICK:
				break;
			case CONNECT_CLICK:
				break;
			case OPTIONS_CLICK:
				break;
		}
		
	}

	@Override
	public void enter(GameContainer container, StateBasedGame game) throws SlickException {
	}

	@Override
	public void leave(GameContainer container, StateBasedGame game) throws SlickException {
	}
}
